package controller

import (
	"net/http"
	"strings"

	"test-gin/entity"
	"test-gin/service"

	"github.com/gin-gonic/gin"
	"gopkg.in/yaml.v3"
)

// InitI18nController 初始化i18n控制器
func InitI18nController(r *gin.Engine) {
	i18nGroup := r.Group("/test-gin/i18n")
	{
		// 保存国际化翻译
		i18nGroup.POST("/translation", SaveTranslation)
		// 添加支持的语言
		i18nGroup.POST("/language", AddLanguage)
		// 获取支持的语言
		i18nGroup.GET("/languages", GetSupportedLanguages)
		// 按app和语言下载翻译
		i18nGroup.GET("/download/:app/:language", DownloadTranslations)
		// 获取所有翻译
		i18nGroup.GET("/translations", GetAllTranslations)
		// 搜索翻译
		i18nGroup.GET("/translations/search", SearchTranslations)
		// 删除单个翻译
		i18nGroup.DELETE("/translation", DeleteTranslation)
		// 批量删除翻译
		i18nGroup.DELETE("/translations/batch", BatchDeleteTranslations)
	}
}

// SaveTranslationRequest 保存翻译请求结构体
type SaveTranslationRequest struct {
	App      string `json:"app" binding:"required"`
	Server   string `json:"server" binding:"required"`
	Key      string `json:"key" binding:"required"`
	Language string `json:"language" binding:"required"`
	Value    string `json:"value" binding:"required"`
}

// SaveTranslation 保存国际化翻译
func SaveTranslation(c *gin.Context) {
	var req SaveTranslationRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	err := service.GetI18nService().SaveTranslation(req.App, req.Server, req.Key, req.Language, req.Value)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Translation saved successfully"})
}

// AddLanguageRequest 添加语言请求结构体
type AddLanguageRequest struct {
	Code string `json:"code" binding:"required"`
	Name string `json:"name" binding:"required"`
}

// AddLanguage 添加支持的语言
func AddLanguage(c *gin.Context) {
	var req AddLanguageRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	err := service.GetI18nService().AddLanguage(req.Code, req.Name)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Language added successfully"})
}

// GetSupportedLanguages 获取支持的语言
func GetSupportedLanguages(c *gin.Context) {
	languages, err := service.GetI18nService().GetSupportedLanguages()
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"languages": languages})
}

// DownloadTranslations 按app和语言下载翻译
func DownloadTranslations(c *gin.Context) {
	app := c.Param("app")
	language := c.Param("language")
	format := c.DefaultQuery("format", "json") // 默认json格式

	// 获取翻译数据
	translations, err := service.GetI18nService().GetTranslationsByAppAndLanguageGrouped(app, language)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	// 设置响应头
	c.Header("Content-Disposition", "attachment; filename=translations."+format)

	// 根据格式返回数据
	switch strings.ToLower(format) {
	case "json":
		c.JSON(http.StatusOK, translations)
	case "yaml", "yml":
		yamlData, err := yaml.Marshal(translations)
		if err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to marshal YAML"})
			return
		}
		c.Data(http.StatusOK, "application/x-yaml", yamlData)
	default:
		c.JSON(http.StatusBadRequest, gin.H{"error": "Unsupported format. Supported formats: json, yaml, yml"})
	}
}

// GetAllTranslations 获取所有翻译
func GetAllTranslations(c *gin.Context) {
	translations, err := service.GetI18nService().GetAllTranslations()
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"translations": translations})
}

// SearchTranslations 搜索翻译
func SearchTranslations(c *gin.Context) {
	app := c.Query("app")
	server := c.Query("server")
	language := c.Query("language")

	translations, err := service.GetI18nService().SearchTranslations(app, server, language)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"translations": translations})
}

// DeleteTranslationRequest 删除翻译请求结构体
type DeleteTranslationRequest struct {
	App      string `json:"app" binding:"required"`
	Server   string `json:"server" binding:"required"`
	Key      string `json:"key" binding:"required"`
	Language string `json:"language" binding:"required"`
}

// DeleteTranslation 删除单个翻译
func DeleteTranslation(c *gin.Context) {
	var req DeleteTranslationRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	err := service.GetI18nService().DeleteTranslation(req.App, req.Server, req.Key, req.Language)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Translation deleted successfully"})
}

// BatchDeleteTranslationsRequest 批量删除翻译请求结构体
type BatchDeleteTranslationsRequest struct {
	Translations []entity.I18nEntity `json:"translations" binding:"required"`
}

// BatchDeleteTranslations 批量删除翻译
func BatchDeleteTranslations(c *gin.Context) {
	var req BatchDeleteTranslationsRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	err := service.GetI18nService().BatchDeleteTranslations(req.Translations)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Translations deleted successfully"})
}
